<?php
/*
 * @分销商城
 * @功能说明：财务登陆
 * @更新说明：暂无更新
 * @文件名 UserController.class.php
 * @编码 UTF-8
 * @创建时间 2016-4-12 下午4:02:24
 * @创建人 XING
 */

namespace pc\Controller;
use Think\Controller;
class UserController extends Controller {
	/*
	 * 分销商城_登陆服务
	 * @date: 2015-12-7 下午4:27:29
	 * @editor: XING
	 */
	public function login(){
        if(is_login()){
            $this->redirect('/index/index');
        }
    	$this->display('user/login');
    }
    /*
     * 护士注册
     * @date: 2017-3-17 下午4:27:29
     */
    public function audit(){
        if(I('post.is_register')){
            if(I('post.mobile')){
                $repeat_m = M('NurseEntity')->where(['mobile'=>I('post.mobile')])->find();
                if($repeat_m){
                    $this->error('该手机号已注册','/user/login');
                }
            }
            if(I('post.card')){
                $repeat_c = M('NurseEntity')->where(['card'=>I('post.card')])->find();
                if($repeat_c){
                    $this->error('该身份证号已注册','/user/login');
                }
            }
            $salt = randstr(5);
            $data = array(
                'uname' => I('post.uname'),
                'mobile' => I('post.mobile'),
                'card' => I('post.card'),
                'salt' => $salt,
                'password' => md5(I('post.password').$salt),
                'add_at' => time(),
                'update_at' => time(),
            );
            if(M('NurseEntity')->add($data)){
                $this->assign('mobile',I('post.mobile'));
                $this->success('','/user/login');
            }
        }
//  else{
//            if(I('get.mobile')){
//                $user = M('NurseEntity')->where(['mobile'=>I('get.mobile')])->find();
//                if($user['checked']){
//                    $this->display('user/login');
//                }else{
//                    $this->assign('mobile',$user['mobile']);
//                    $this->display('user/audit');
//                }
//            }
//        }
        $this->display('user/audit');
    }
    
	/*
	 * 分销商城_校验登陆服务
	 * @date: 2015-12-7 下午4:28:31
	 * @editor: XING
	 */
    public function checklogin(){
	if(!I('post.mobile') || !I('post.password')){
            dataFormat('账号密码不能为空', '1', 'error');
        }
    	$userMsg = M('NurseEntity')
    			 ->where('mobile = '.trim(I('post.mobile')))->find();
        if(!$userMsg['checked'] && $userMsg['user_status']){
            dataFormat('您的帐号未审核通过或账户状态不对，请联系客服！', '1', 'error');
        }
    	if($userMsg['password'] == md5(I('post.password').$userMsg['salt'])){

            if(I('post.signlogin') == 1){
                $setSecret = md5(I('post.uname').'ajwUid'.time());
                M('NurseEntity')->where(array('mobile'=>I('post.mobile')))->setField(
                    array('remember_token' => $setSecret)
                );
                cookie('_ajwunion',$setSecret,'expire=604800');
                $pushSession['expire'] = 604800;
            }
    		session('NurseUid',$userMsg['id']);
    		session('NurseName',$userMsg['uname']);
    		session('NurseMobile',$userMsg['mobile']);
            dataFormat(['url'=>'/index/index'], '0', 'success');
    	}else{
            dataFormat('帐号密码有误，请重试', '1', 'error');
    	}
    }
    
	/*
	 * 分销商城_帐号退出
	 * @date: 2015-12-7 下午4:28:42
	 * @editor: XING
	 */
    public function loginout(){
    	session(cookie('PHPSESSID'),null);
    	session('NurseName',null);
    	session('NurseUid',null);
    	session('NurseMobile',null);
    	cookie('PHPSESSID',null);
    	session_destroy();

    	redirect('/user/login');
    }
    
    /*
     * 个人中心简介
     * @date: 2015-3-23下午2:30:20
     * @editor: XING
     */
    public function usercenter(){
    	//个人页面渲染
    	$this->display('user/usercenter');
    }
    
    /*
     * 个人中心资料编辑
     * @date: 2015-3-23下午5:06:13
     * @editor: XING
     */
    public function userUpdate(){
        if(I('post.password') == I('post.new_password')){
    		$where = array('id'=>session('NurseUid'));
            $salt = randstr(5);
            $data['password'] = md5(I('post.password').$salt);
            $data['salt'] = $salt;
    		$rs = M('NurseEntity')->where($where)->setField($data);
    		if($rs){
                dataFormat(['msg'=>'密码已重置成功，请重新登录', 'url'=>'/user/loginout'], '0', 'success');
            }
    	}else{
            dataFormat(['msg'=>'帐号密码有误，请重试', 'url'=>'/index/index'], '1', 'error');
        }
    }
    
}
